﻿using System.Threading.Tasks;
using Red.Controls.Diagrams.Declarators;
using Red.Controls.ViewModels;

namespace Red.Controls.Diagrams.InteropUtils
{
    public class InteropNodeDownloader
    {
        #region Data

        private readonly DataItem _parentDataItem;
        private readonly InteropObjectInitializer _initializer;
        private readonly string _subPath;
        private InteropObject _interopObject;

        #endregion Data

        #region C-tor

        public InteropNodeDownloader( DataItem parentDataItem, string nodeId, NodeDeclarators declarators )
        {
            _initializer = new InteropObjectInitializer( declarators );
            _subPath = "nodesMap[\"" + nodeId + "\"]";
            _parentDataItem = parentDataItem;
        }

        #endregion C-tor

        #region Public methods

        public async Task<InteropObject> Execute()
        {
            var nodeDataItem = await _parentDataItem.DigUpSubItemAsync( _subPath );
            _interopObject = new InteropObject( nodeDataItem );
            await _interopObject.Accept( _initializer );
            return _interopObject;
        }

        #endregion Public methods
    }
}
